package com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.ui.homeparent;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SearchView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.R;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.databinding.FragmentHomeParentBinding;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.model.UserViewModel;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.utils.GlobalViewModelStore;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.utils.MyApplication;

public class HomeParentFragment extends Fragment {

    private FragmentHomeParentBinding binding;
    private HomeParentViewModel homeParentViewModel;
    private UserViewModel userViewModel;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 使用带参构造函数创建 HomeParentViewModel
        homeParentViewModel = new ViewModelProvider(this, new HomeParentViewModel.Factory(requireContext())).get(HomeParentViewModel.class);
//        homeParentViewModel = new ViewModelProvider(this).get(HomeParentViewModel.class);
        binding = FragmentHomeParentBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        setUserViewModel();
        setupSearchView();
        setupButtonClickListeners();

        return root;
    }

    private void setUserViewModel(){
        //用户身份
        // 获取全局共享的 UserViewModel
        userViewModel = GlobalViewModelStore
                .getInstance(MyApplication.getInstance())
                .get(UserViewModel.class);
    }

    private void setupSearchView() {
        SearchView searchView = binding.shvSearchDoctor;
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                homeParentViewModel.performSearch(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // 可以根据需求添加输入文本变化时的逻辑
                return false;
            }
        });
    }

    private void setupButtonClickListeners() {
        // 检查布局文件中按钮 id 是否为 btn_1 等，如果是则修改此处
        Button btn1 = binding.btn1;
        Button btn2 = binding.btn2;
        Button btn3 = binding.btn3;
        Button btn4 = binding.btn4;
        Button btn5 = binding.btn5;
        Button btn6 = binding.btn6;

        btn1.setOnClickListener(v -> openMedicineReminderFragment());
        btn2.setOnClickListener(v -> homeParentViewModel.onButtonClicked(2));
        btn3.setOnClickListener(v -> homeParentViewModel.onButtonClicked(3));
        btn4.setOnClickListener(v -> {
            // 点击爱心服务按钮，导航到 AIFragment
            NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment_activity_home_parent);
            navController.navigate(R.id.navigation_ai);
        });
        btn5.setOnClickListener(v -> {
            // 点击健康科普按钮，导航到 CommunityFragment
            NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment_activity_home_parent);
            navController.navigate(R.id.navigation_community);

        });
        btn6.setOnClickListener(v -> homeParentViewModel.onButtonClicked(6));
    }

    public void openMedicineReminderFragment() {
        try {
            NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment_activity_home_parent);
            navController.navigate(R.id.navigation_medicinereminder);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}